package com.he.DesignPatterns.template.demo1;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.he.entity.Book;
import com.he.entity.NameInfo;
import com.he.entity.NewBook;
import com.he.mapper.BookMapper;
import com.he.mapper.NameInfoMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: TODO
 * @Author: HCL
 * @Date: 2024/8/28 16:55
 */
@Service("NameInfoService")
public class NameInfoServiceImpl implements BookAbstract{


    @Resource
    private BookMapper bookMapper;

    @Resource
    private NameInfoMapper nameInfoMapper;

    @Override
    public List<Book> queryData() {
        return bookMapper.selectList(
                new LambdaUpdateWrapper<Book>()
                        .eq(Book::getStatus, 0)
        );
    }

    //应该设置成泛型，目前暂时使用
    @Override
    public List<NewBook> adjustData(List<Book> bookList) {
        bookList.forEach(o -> bookMapper.update(null,
                new LambdaUpdateWrapper<Book>()
                        .set(Book::getStatus, 1)
                        .eq(Book::getId, o.getId()))
        );
        List<NewBook> newBookList = bookList.stream().map(o -> {
            NewBook newBook = new NewBook();
            BeanUtil.copyProperties(o, newBook);
            return newBook;
        }).collect(Collectors.toList());
        return newBookList;
    }

    //应该设置成泛型，目前暂时使用
    @Override
    public void insertData(List<NewBook> newBookList) {
        List<NameInfo> nameInfoList = newBookList.stream().map(o -> {
            NameInfo nameInfo = new NameInfo();
            BeanUtil.copyProperties(o, nameInfo);
            return nameInfo;
        }).collect(Collectors.toList());
        nameInfoList.forEach(o->nameInfoMapper.insert(o));
    }
}
